<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Comment_model extends WST_Model {
    private $_table = "comments";

    function __contruct(){
        parent::__construct();
    }

    public function get_list_comment_post($post_id,$flag=false){
        $getByCommentPost = $this->memcached->get('all_comment_post_'.$post_id);
        if($getByCommentPost===false || $flag===true){
            $data=array();
            $data = $this->getTreeArray($post_id);
            if(count($data)>0){
                $getByCommentPost = array();
                foreach ($data as $val) {
                    $getByCommentPost[$val['id']] = $val;
                    $getByCommentPost[$val['id']]['_childrens'] = $this->getTreeArray($post_id,$val['id']);
                }
            }
            $this->memcached->add('all_comment_post_'.$post_id,$getByCommentPost,null);
        }
        return $getByCommentPost;
    }
    public function getTreeArray($post_id = 0,$parent = null)
    {
        $data=array();
        if (is_null($parent)) {
            $this->db->where('parent',0);
            $this->db->order_by('id','DESC');
        } else {
            $this->db->where('parent',$parent);
            $this->db->order_by('id','ASC');
        }
        $this->db->where('post_id',$post_id);

        $query = $this->db->get($this->_table);
        $data=$query->result_array();
        $query->free_result();
        return $data;
    }
}